package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService{

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public SetmealVO getSetMealById(Long id) {
        Setmeal setmeal = this.getById(id);
        if(setmeal==null)
            return null;
        SetmealVO setMealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setMealVO);
        List<SetmealDish>setmealDishes = new ArrayList<>();
        setmealDishes = setmealMapper.getSetmealDishes(id);
        setMealVO.setSetmealDishes(setmealDishes);
        return setMealVO;
    }

    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        List<SetmealDish>setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList!=null)
           for(int i=0;i<setmealDishList.size();++i)
               setmealDishList.get(i).setSetmealId(setmeal.getId());
        setmealDishService.saveBatchs(setmealDTO.getSetmealDishes());
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal>page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void updateWithDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateById(setmeal);
        //采取先删除后新增的方式修改 减少麻烦
        //使用mybatis-plus的条件构造器删除
        LambdaQueryWrapper<SetmealDish>setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setMealDishMapper.delete(setmealDishLambdaQueryWrapper);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for(int i=0;i<setmealDishes.size();++i)
            setmealDishes.get(i).setSetmealId(setmeal.getId());
        setmealDishService.saveBatchs(setmealDishes);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if(ids==null||ids.size()==0)
            return;
        //删除套餐数据
        this.removeBatchByIds(ids);
        //删除套餐和菜品的关联数据
        for(int i=0;i<ids.size();++i){
            Long id = ids.get(i);
            LambdaQueryWrapper<SetmealDish>setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
            setMealDishMapper.delete(setmealDishLambdaQueryWrapper);
        }
    }
}
